package net.wisos.opn.cms.service;

import net.wisos.opn.cms.dao.ArticleDao;
import net.wisos.opn.cms.dao.ChannelDao;
import net.wisos.opn.cms.entity.News;
import net.wisos.opn.cms.entity.Channel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional(readOnly = true)
public class ArticleManager {
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ChannelDao channelDao;
	
	

	// -- News Manager --//
	public News getArticle(Long id) {
		return articleDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveArticle(News article) {
		articleDao.save(article);
	}

	@Transactional(readOnly = false)
	public void deleteArticle(Long id) {
		articleDao.delete(id);
	}

	public Page<News> findArticleByChannel(Channel channel, Pageable pageable) {
		return articleDao.findByChannel(channel, pageable);
	}

	// -- Channel Manager --//
	public Channel getChannel(Long id) {
		return channelDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveChannel(Channel channel) {
		channelDao.save(channel);
	}

	@Transactional(readOnly = false)
	public void deleteChannel(Long id) {
		channelDao.delete(id);
	}

}
